<?php

declare(strict_types=1);

namespace App\Utils;

use App\Utils\Control\ActionUtil;
use Hyperf\Context\ApplicationContext;
use Hyperf\HttpServer\Contract\ResponseInterface;
use Psr\Http\Message\ResponseInterface as Psr7ResponseInterface;

/**
 * Author:Panda
 * Email:joeyoung0314@qq.com
 * Class AppResult
 * @package App\Utils
 */
class AppResult
{

    /**==============应用返回封装==============**/

    /**
     * 成功返回200
     * @param string $message
     * @param $result
     * @return Psr7ResponseInterface
     * @throws \Psr\Container\ContainerExceptionInterface
     * @throws \Psr\Container\NotFoundExceptionInterface
     */
    public static function response200(string $message = 'success', $result = null)
    {
        return self::response([
            'code'    => 200,
            'message' => $message,
            'result'  => $result
        ]);
    }

    /**
     * 失败返回201
     * @param string $message
     * @param $result
     * @return Psr7ResponseInterface
     * @throws \Psr\Container\ContainerExceptionInterface
     * @throws \Psr\Container\NotFoundExceptionInterface
     */
    public static function response201(string $message = 'error', $result = null)
    {
        return self::response([
            'code'    => 201,
            'message' => $message,
            'result'  => $result
        ]);
    }

    /**
     * 失败返回202
     * @param string $message
     * @param $result
     * @return Psr7ResponseInterface
     * @throws \Psr\Container\ContainerExceptionInterface
     * @throws \Psr\Container\NotFoundExceptionInterface
     */
    public static function response202(string $message = 'error', $result = null)
    {
        return self::response([
            'code'    => 202,
            'message' => $message,
            'result'  => $result
        ]);
    }

    /**
     * 参数缺失返回203
     * @param string $message
     * @param $result
     * @return Psr7ResponseInterface
     * @throws \Psr\Container\ContainerExceptionInterface
     * @throws \Psr\Container\NotFoundExceptionInterface
     */
    public static function response203(string $message = 'error', $result = null)
    {
        return self::response([
            'code'    => 203,
            'message' => $message,
            'result'  => $result
        ]);
    }

    /**
     * 授权无用户返回204
     * @param string $message
     * @param $result
     * @return Psr7ResponseInterface
     * @throws \Psr\Container\ContainerExceptionInterface
     * @throws \Psr\Container\NotFoundExceptionInterface
     */
    public static function response204(string $message = '暂无此用户，请先授权手机号，并登录', $result = null)
    {
        return self::response([
            'code'    => 204,
            'message' => $message,
            'result'  => $result
        ]);
    }

    /**
     * 请求次数过多返回206
     * @param string $message
     * @param $result
     * @return Psr7ResponseInterface
     * @throws \Psr\Container\ContainerExceptionInterface
     * @throws \Psr\Container\NotFoundExceptionInterface
     */
    public static function response206(string $message = '请求次数过多', $result = null)
    {
        return self::response([
            'code'    => 206,
            'message' => $message,
            'result'  => $result
        ]);
    }

    /**
     * 身份信息已过期400
     * @param string $message
     * @param $result
     * @return Psr7ResponseInterface
     * @throws \Psr\Container\ContainerExceptionInterface
     * @throws \Psr\Container\NotFoundExceptionInterface
     */
    public static function response400(string $message = 'Identity is overdue', $result = null)
    {
        return self::response([
            'code'    => 400,
            'message' => $message,
            'result'  => $result
        ]);
    }

    /**
     * 致命错误500
     * @param string $message
     * @param $result
     * @return Psr7ResponseInterface
     * @throws \Psr\Container\ContainerExceptionInterface
     * @throws \Psr\Container\NotFoundExceptionInterface
     */
    public static function response500(string $message = 'Fatal error', $result = null)
    {
        return self::response([
            'code'    => 500,
            'message' => $message,
            'result'  => $result
        ]);
    }

    /**
     * 权限未拥有777
     * @param string $message
     * @param $result
     * @return Psr7ResponseInterface
     * @throws \Psr\Container\ContainerExceptionInterface
     * @throws \Psr\Container\NotFoundExceptionInterface
     */
    public static function response777(string $message = 'No Access', $result = null)
    {
        return self::response([
            'code'    => 777,
            'message' => $message,
            'result'  => $result
        ]);
    }

    /**
     * 统一返回格式
     * @param $data
     * @return Psr7ResponseInterface
     * @throws \Psr\Container\ContainerExceptionInterface
     * @throws \Psr\Container\NotFoundExceptionInterface
     */
    private static function response(array $data){
        // 处理返回结果
        $response = self::container()->get(ResponseInterface::class);
        $response = $response->json($data);

        // 记录日志
        $action = ActionUtil::getInstance()->get();
        if (!empty($action)){
            if (!empty($data['result']) && strlen(json_encode($data['result'])) > 1000){
                $data['result'] = '数据太多，不记录';
            }
            LogUtil::info('响应结果', $action['controller'], $action['action'],$data);
        }

        return $response;
    }

    /**
     * 容器实例
     * @return \Psr\Container\ContainerInterface
     */
    private static function container()
    {
        return ApplicationContext::getContainer();
    }
}